package com.java110.mall.chain.supplier.express;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.storeOrder.OrderExpressDto;
import com.java110.dto.storeOrder.StoreOrderLogisticsDto;
import com.java110.mall.chain.supplier.IExpressAdapter;
import com.java110.mall.chain.supplier.print.YilanyunOrderPrintAdapter;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.contant.CompanyConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 快递100
 * https://api.kuaidi100.com/document/5f0ffb5ebc8da837cbd8aefc#section_2
 */
@Service("kuaiDi100ExpressAdapter")
public class KuaiDi100ExpressAdapter implements IExpressAdapter {

    private static Logger logger = LoggerFactory.getLogger(KuaiDi100ExpressAdapter.class);

    public static final String PREFIX = "KUAIDI100";


    @Override
    public List<OrderExpressDto> getOrderExpress(StoreOrderLogisticsDto storeOrderLogisticsDto) {

        String customer = MappingCache.getValue("KUAIDI100", "kuaidi_customer");
        String key = MappingCache.getValue("KUAIDI100", "kuaidi_key");
        List<OrderExpressDto> orderExpressDtos = null;
        String resStr = CommonCache.getValue(PREFIX + storeOrderLogisticsDto.getSolNum());
        if (!StringUtil.isEmpty(resStr)) {
            orderExpressDtos = getResultExpressDto(resStr);
            return orderExpressDtos;
        }

        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(storeOrderLogisticsDto.getSolCompany());
        queryTrackParam.setNum(storeOrderLogisticsDto.getSolNum());
        //queryTrackParam.setPhone("17725390266");
        String param = JSONObject.toJSONString(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param, key, customer));

        IBaseClient baseClient = new QueryTrack();
        HttpResult result = null;
        try {
            result = baseClient.execute(queryTrackReq);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        logger.debug("-------------------调用易联云打印机返回日志：{}", result);

        resStr = result.getBody();
        JSONObject paramOut = JSONObject.parseObject(resStr);
        if (!"200".equals(paramOut.getString("status"))) {
            throw new IllegalArgumentException(paramOut.getString("message"));
        }
        CommonCache.setValue(PREFIX + storeOrderLogisticsDto.getSolNum(), resStr, 40 * 60);
        orderExpressDtos = getResultExpressDto(resStr);

        return orderExpressDtos;
    }

    private static List<OrderExpressDto> getResultExpressDto(String resStr) {
        JSONObject paramOut = JSONObject.parseObject(resStr);
        JSONArray data = paramOut.getJSONArray("data");
        List<OrderExpressDto> orderExpressDtos = new ArrayList<>();
        OrderExpressDto orderExpressDto = null;
        for (int dataIndex = 0; dataIndex < data.size(); dataIndex++) {
            orderExpressDto = BeanConvertUtil.covertBean(data.getJSONObject(dataIndex), OrderExpressDto.class);
            orderExpressDtos.add(orderExpressDto);
        }
        return orderExpressDtos;
    }
}
